package com.jiegui.web.controller.system;

import com.jiegui.common.annotation.Log;
import com.jiegui.common.core.controller.BaseController;
import com.jiegui.common.core.domain.AjaxResult;
import com.jiegui.common.core.page.TableDataInfo;
import com.jiegui.common.enums.BusinessType;
import com.jiegui.common.exception.user.RRException;
import com.jiegui.common.utils.poi.ExcelUtil;
import com.jiegui.framework.oss.AliyunCloudStorageService;
import com.jiegui.framework.oss.CloudStorageConfig;
import com.jiegui.framework.oss.CloudStorageService;
import com.jiegui.system.domain.SysOss;
import com.jiegui.system.service.ISystemConfigService;
import com.jiegui.system.service.SysOssService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文件上传Controller
 * 
 * @author wuqicheng
 * @date 2022-01-11
 */
@RestController
@RequestMapping("/system/sysOss")
public class SysOssController extends BaseController
{
    @Resource
    private SysOssService sysOssService;

    @Resource
    ISystemConfigService systemConfigService;

    /**
     * 查询文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysOss:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOss sysOss)
    {
        startPage();
        List<SysOss> list = sysOssService.selectSysOssList(sysOss);
        return getDataTable(list);
    }

    /**
     * 导出文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysOss:export')")
    @Log(title = "文件上传", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysOss sysOss)
    {
        List<SysOss> list = sysOssService.selectSysOssList(sysOss);
        ExcelUtil<SysOss> util = new ExcelUtil<SysOss>(SysOss.class);
        return util.exportExcel(list, "文件上传数据");
    }

    /**
     * 获取文件上传详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysOss:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysOssService.selectSysOssById(id));
    }

    /**
     * 新增文件上传
     */
    @PreAuthorize("@ss.hasPermi('system:sysOss:add')")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.insertSysOss(sysOss));
    }

    /**
     * 修改文件上传
     */
    @PreAuthorize("@ss.hasPermi('system:sysOss:edit')")
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.updateSysOss(sysOss));
    }

    /**
     * 图片上传
     */
    @RequestMapping("/upload")
    public AjaxResult Multiplechoice(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

       // String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        CloudStorageConfig config = systemConfigService.getConfigObject("CLOUD_STORAGE_CONFIG_KEY", CloudStorageConfig.class,true);
        CloudStorageService cloudStorageService = new AliyunCloudStorageService(config);
        String url = Objects.requireNonNull(cloudStorageService).uploadSuffix(file.getBytes(), suffix);
        //保存文件信息
        SysOss ossEntity = new SysOss();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.save(ossEntity);
       return AjaxResult.success("data",url);
    }
}
